package com.lmx.repoDomain.repository;

import com.lmx.persistence.service.TenantService;
import com.lmx.repoDomain.tmodel.Tenant;
import com.lmx.repoDomain.tmodel.TenantEvaModelSource;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@JsonApiResourceRepository(TenantEvaModelSource.class)
@Component
public class TenantEvaModelSourceRepository extends BaseRepository<TenantEvaModelSource> {

	@Autowired
	private TenantService<Tenant> tenantService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(TenantEvaModelSource.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	@JsonApiSave
	public TenantEvaModelSource save(TenantEvaModelSource tenantModelSource) {
		if (tenantModelSource.getRemark() != null && "tenant".equals(tenantModelSource.getRemark())) {
			return tenantService.addModeSourceData(tenantModelSource, tenantModelSource.getTenant(),
					tenantModelSource.getModelSource());
		}
		return super.save(tenantModelSource);
	}
}
